/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.widgets.adapters;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.phonebooksharing.android.R;
import com.phonebooksharing.android.widgets.header.ListWithHeaderHelper;

public class GenericExpandableListAdapter extends BaseExpandableListAdapter {
    
    protected String[] groupNames;
    protected HashMap<String, List<Map<String, ?>>> entryMap;
    protected LayoutInflater inflater;
    
    public GenericExpandableListAdapter(Context c, HashMap<String, List<Map<String, ?>>> map) {
        groupNames = map.keySet().toArray(new String[] {});
        entryMap = map;
        inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View cv, ViewGroup parent) {
        if(cv == null)
            cv = inflater.inflate(R.layout.list_item_3lines, parent, false);
        
        final Map<String, ?> item = entryMap.get(groupNames[groupPosition]).get(childPosition);
        
        ((ImageView)cv.findViewById(android.R.id.icon)).setImageResource((Integer)item.get(ListWithHeaderHelper.ITEM_ICON));
        ((TextView)cv.findViewById(android.R.id.text1)).setText(item.get(ListWithHeaderHelper.ITEM_TITLE).toString());
        ((TextView)cv.findViewById(R.id.text3)).setText(item.get(ListWithHeaderHelper.ITEM_CAPTION).toString());
        ((TextView)cv.findViewById(R.id.text2)).setText((String)item.get(ListWithHeaderHelper.ITEM_COUNTER));
        ((TextView)cv.findViewById(R.id.text4)).setText((String)item.get(ListWithHeaderHelper.ITEM_LINE2));
        
        return cv;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return entryMap.get(groupNames[groupPosition]).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupNames[groupPosition];
    }

    @Override
    public int getGroupCount() {
        return groupNames.length;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = inflater.inflate(R.layout.exp_2column, parent, false);

        String group = (String) this.getGroup(groupPosition);
        ((TextView) convertView.findViewById(android.R.id.text1)).setText(group);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}